package com.gxa.controller;

import com.gxa.pojo.CustomerType;
import com.gxa.service.CustomerTypeService;
import com.gxa.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerType")
@Api(value = "执行客户类型操作的控制器", tags = "客户类型接口的api")
@CrossOrigin
public class CustomerTypeController {

    @Autowired
    CustomerTypeService customerTypeService;

    // 添加
    @ResponseBody
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    @ApiOperation(value = "添加客户类型数据的接口", notes = "客户类型添加", httpMethod = "post", response = String.class)
    public Map insert(@RequestBody Map map){
        int i = customerTypeService.insert(map);
        JsonResult jsonResult = new JsonResult();
        if (i>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("添加成功！");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开小差了！");
        }
        return jsonResult.getValues();

    }

    // 删除
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ApiOperation(value = "删除客户类型的接口", notes = "客户类型删除", httpMethod = "post", response = String.class)
    public Map delete(@RequestBody Map<String,Object> s){
        Integer id =(Integer) s.get("id");

        int i = customerTypeService.deleteByPrimaryKey(id);
        JsonResult jsonResult = new JsonResult();
        if (i>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功！");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("删除失败！");
        }
        return jsonResult.getValues();

    }

    // 批量删除
    @ResponseBody
    @RequestMapping(value = "/deleteBatch",method = RequestMethod.POST)
    @ApiOperation(value = "批量删除客户类型的接口", notes = "客户类型批量删除", httpMethod = "post", response = String.class)
    public Map deleteBatch(@RequestBody Map<String,List> s){
        List ids = s.get("id");
        int i = customerTypeService.deleteBatch(ids);
        JsonResult jsonResult = new JsonResult();
        if (i>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功！");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开小差了！");
        }
        return jsonResult.getValues();
    }

    // 修改
    @ResponseBody
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation(value = "修改客户类型的接口", notes = "客户类型修改", httpMethod = "post", response = String.class)
    public Map update(@RequestBody Map map){
        Integer id = (Integer) map.get("id");
        map.remove("id");
        map.put("id",id);
        int i=customerTypeService.updateByPrimaryKeySelective(map);
        JsonResult jsonResult = new JsonResult();
        if (i>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("修改成功！");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("修改失败！");
        }
        return jsonResult.getValues();
    }

    // 分页查询
    @ResponseBody
    @RequestMapping(value = "/findAllCustomerTypeByPage",method = RequestMethod.POST)
    @ApiOperation(value = "分页查询客户类型的接口", notes = "客户类型分页查询", httpMethod = "post", response = String.class)
    public Map findAllCustomerTypeByPage(@RequestBody Map map){
        String number=map.get("number").toString();     //当前页数 1
        String size=map.get("size").toString();         // 分页大小 5
        Integer offset = (Integer.parseInt(number) - 1) * Integer.parseInt(size);
        Integer limit = Integer.parseInt(size);     // limit=size
        List<CustomerType> customerTypes = customerTypeService.findAllCustomerTypeByPage(offset, limit);
        Integer count = customerTypeService.findAllCustomerType();  // 数据的总条数
        //计算总页数
        int totalPage=count/limit;
        if (count%limit==0){
            totalPage=count/limit;
        }else {
            totalPage=count/limit+1;
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(customerTypes);
        //jsonResult.setData("totalPage:"+totalPage);
        jsonResult.put("count",count);
        jsonResult.put("totalPage",totalPage);
        return jsonResult.getValues();
    }

    // 查询所有客户类型名称
    @ResponseBody
    @RequestMapping(value = "/findCustomerTypeName",method = RequestMethod.POST)
    @ApiOperation(value = "查询所有客户类型名称的接口", notes = "查询所有客户类型名称", httpMethod = "post", response = String.class)
    public Map <String,Object>findCustomerTypeName(){
        List<Map<String, Object>> customerTypeName = customerTypeService.findCustomerTypeName();
        JsonResult jsonResult = new JsonResult();
        if (customerTypeName!=null){
            jsonResult.setCode("200");
            jsonResult.setData(customerTypeName);
            jsonResult.setMsg("查询成功！");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("查询失败！");
        }
        return jsonResult.getValues();
    }



}
